1. Field of the Invention
The present invention generally relates to a method and an apparatus for encrypting/decrypting packet data, and more particularly, to a method and an apparatus for encrypting/decrypting packet data of a precise time synchronization protocol.
2. Description of Related Art
At times, or sometimes, nodes of a network need to synchronize their time with each other to complete a specific task together. Thus, different network time synchronization methods have been provided, wherein the most common methods include the network time protocol (NTP), the real-time transport protocol (RTP), and the precise time protocol (PTP) defined by the IEEE 1588 standard.
The RTP and NTP are used for time synchronization on the application layer, while the PTP is used for time synchronization on the network layer. Accordingly, the time delay of the PTP is shorter than that of the RTP, and the time precision of the PTP is much higher than those of the NTP and the RTP. The time delay of the NTP and the RTP is about equal to the time delay from the physical layer, the network driving program, the operating system core, until the application program.
A best master clock (BMC) algorithm is specified by the PTP. Each node executes the BMC algorithm by comparing a received PTP packet message with its own local clock to determine whether the node itself could be the best master clock. There could be only one master node among all the nodes to be synchronized. All the slave nodes synchronize their time with the master node, and the master node issues a time synchronization signal to the slave nodes every two seconds to maintain the time synchronization constantly.
In an automatic control application, a time synchronization system requires equipments having high isochronism in order to sort the working order of these automatic equipments. For example, the isochronous real-time (IRT) technique for synchronizing robot arms, motor speed controller and console microcomputers can meet the high-speed communication requirement of movement control. When there are 1000 nodes, the response time required by a time synchronization system should be shorter than 1 μs, and the jitter error should be less than 10 ns in order to ensure real-time and accurate responses. Thereby, most time synchronization systems adopt the PTP for carrying out time synchronization.
The security of packet data has to be ensured when a PTP packet (also referred to as an isochronal packet) is transmitted. An unencrypted PTP packet may be stolen or eavesdropped by a network hacker, and the hacker may further obtain the control right over the entire time synchronization system or interrupt the synchronization time of nodes in the entire network by altering the time stamp in the PTP packet. Thus, if a time synchronization system adopted by an automatic equipment application is hacked by a hacker, the originally sorted mass-production flow will be disturbed and accordingly a big loss will be caused to the company or factory.
FIG. 1 is a schematic diagram illustrating how conventionally a PTP packet is encrypted and decrypted. Referring to FIG. 1, the conventional time synchronization system 10 includes a master node 100 and a slave node 101, wherein the master node 100 and the slave node 101 both adopt the User Datagram Protocol/Internet Protocol (UDP/IP).
First, when the master node 100 is about to transmit a PTP packet 110, the master node 100 first executes a PTP application program on the PTP application layer. The PTP application program calls the central processing unit (CPU) to execute an encryption program for encrypting the synchronization data and the time stamp and packages the encrypted synchronization data and time stamp into the PTP packet 110. The PTP packet 110 includes a PTP authentication header 111, a key 112, a PTP message 113, and a filling sample 114. Then, the PTP packet 110 is sent to the UDP layer and packaged into a UDP packet 120 (including a UDP data 122 and a UDP header 121). Next, the UDP packet 120 is sent to the IP layer and packaged into an IP packet 130 (including an IP data 132 and an IP header 131). After that, the IP packet 130 is sent to the medium access control (MAC) layer and packaged into a frame 140 (including a frame data 142 and a frame header 141). Thereafter, the frame 140 is sent to the physical layer, and the master node 100 transmits the frame 140 to the slave node 101 via the physical layer to carry out the time synchronization.
The slave node 101 receives the frame 150 (including a frame data 152 and a frame header 151) from the physical layer and sends the frame 150 to the MAC layer. The MAC layer processes the frame 150 to generate an IP packet 160 (including an IP data 162 and an IP header 161) for the IP layer. Then, the IP layer processes the IP packet 160 to generate a UDP packet 170 (including a UDP data 172 and a UDP header 171) for the UDP layer. After that, the UDP layer processes the UDP packet 170 to generate a PTP packet 180 (including a PTP authentication header 181, a key 182, a PTP message 183, and a filling sample 184) for the PTP application layer. Thereafter, the PTP application layer calls the CPU to execute a decryption program for decrypting the PTP packet 180, so as to obtain the synchronization data and the time stamp.
It should be mentioned that the PTP message contains the time stamp and the synchronization data, etc. The filling sample is used for filling up the unused bits in the PTP packet so that the length of the PTP packet can meet the specified PTP packet length. The PTP authentication header contains information about whether the data is encrypted, information about the length of the key, information about the length of the PTP message, and information about the length of the filling sample.
In the conventional method for encrypting/decrypting a PTP packet, the encryption and decryption operations are carried out on the PTP application layer, and the data is processed and transmitted on many different layers before the slave node obtains the time stamp and the synchronization data and accordingly carries out the time synchronization action. Thus, a delay time and a latency time are produced, since the time stamp obtained by the slave nodes transmitted through a large protocol stack. As a result, the time when the time stamp is obtained by the slave node is delayed for several milliseconds compared to the time when the PTP packet is transmitted by the master node.
It should be noted that the time delay caused by the protocol stack is mainly related to the execution performances of the master node and the slave node, and since the execution performance of the master node and slave node is difficult to estimate, the time delay caused by the protocol stack is difficult to be obtained.
FIG. 2 is a schematic diagram illustrating a conventional time synchronization system hacked by a hacker node. Referring to FIG. 2, the conventional time synchronization system 20 includes a master node 200 and slave nodes 201 and 202, wherein the slave nodes 201 and 202 have to obtain their keys from the PTP packet transmitted by the master node 200. Each time when the master node issues a synchronization signal Sync_M to the slave nodes 201 and 202 to synchronize their time, the slave nodes 201 and 202 decrypt the PTP messages according to the keys to synchronize their time with the master node 200.
If a hacker node 203 steals the encryption/decryption program from the conventional time synchronization system 20, the hacker node 203 can obtain the key and the IP data of the master node. Then, the hacker node 203 may pretend as the master node and randomly issues a synchronization signal Sync_H to request the slave nodes 201 and 202 to synchronize their time with the hacker node 203. As a result, the entire conventional time synchronization system 20 will be broken down.
FIG. 3A is a schematic diagram illustrating how a slave node is added to a conventional time synchronization system. Referring to FIG. 3A, the conventional time synchronization system 30 includes a master node 300 and slave nodes 301 and 302. When a slave node 303 is newly added to the conventional time synchronization system 30, since the slave node 303 has no key, it cannot decrypt the PTP message in the PTP packet and accordingly cannot carry out the time synchronization operation. Instead, the slave node 303 has to wait until the entire conventional time synchronization system 30 resets or when next time the master node 300 sends keys to all the slave nodes 301˜303 to synchronize their time.
FIG. 3B is a schematic diagram illustrating how a master node is removed from a conventional time synchronization system. Referring to FIG. 3B, the conventional time synchronization system 40 includes a master node 400 and slave nodes 401˜403. When the master node 400 is intended to be removed from the conventional time synchronization system 40, each of the slave nodes 401˜403 in the time synchronization system 40 has to execute the BMC algorithm to determine one of the slave nodes 401˜403 as a new master node. The new master node transmits new keys to the slave nodes 402 and 403 (assuming the slave node 401 is determined as the new master node) such that the slave nodes 402 and 403 can decrypt the encrypted PTP messages and synchronize their time with the new master node according to the new keys.
As described above, the conventional time synchronization system has long delay time and low data security. In addition, when a master node is removed or a new slave node is added, time synchronization cannot be carried out instantly. As a result, the expandability of the system is limited.